/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.db.generator;

import net.db.generator.field.models.BaseFieldModel;
import net.db.generator.utils.ClassGeneratorUtil;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author abosancic
 */
public class JavaDAOFullClassWriter extends JavaDAOBaseClassWriter{

    public JavaDAOFullClassWriter() {
        
    }

    @Override
    protected void generateGettersAndSetters() {
        super.generateGettersAndSetters();
        
        generateFindMethods();
        generateRemoveMethods();
    }
    
    protected void generateRemoveMethods() {
        
        stringClass.append(spaces).append("/**\n");
        stringClass.append(spaces).append(" *\n");
        stringClass.append(spaces).append(" * Auto generate remove methods\n");                        
        stringClass.append(spaces).append(" * @author " + System.getProperty("user.name") + "\n");                                
        stringClass.append(spaces).append(" * @date " + new SimpleDateFormat("MMM-dd-yyyy").format(new Date()) + "\n");                                        
        stringClass.append(spaces).append(" */");
        
        for (BaseFieldModel fieldModel : fields) {
            String field = fieldModel.getFieldName();
            String type = fieldModel.getFieldType();
            
            // skip the primary key
            if(uniqueField.equals(field)){
                continue;
            }
                
            String s = "\n"
                    + "    public boolean removeBy" + ClassGeneratorUtil.humanize(field) + "(" + type + " " + ClassGeneratorUtil.humanizeToField(field) + ") {\n"
                    + "        return removeProcessing(\"" + field + "\", " + ClassGeneratorUtil.humanizeToField(field) + ");\n"
                    + "    }";
            
            stringClass.append(s).append("\n");
        }
        
        stringClass.append("\n");
    }

    private void generateFindMethods() {

        stringClass.append(spaces).append("/**\n");
        stringClass.append(spaces).append(" *\n");
        stringClass.append(spaces).append(" * Auto generate find methods\n");
        stringClass.append(spaces).append(" * @author " + System.getProperty("user.name") + "\n");
        stringClass.append(spaces).append(" * @date " + new SimpleDateFormat("MMM-dd-yyyy").format(new Date()) + "\n");
        stringClass.append(spaces).append(" */");

        for (BaseFieldModel fieldModel : fields) {
            String field = fieldModel.getFieldName();
            String type = fieldModel.getFieldType();
            
            // skip the primary key
            boolean skipPrimary = false;
            
            for (String unfield : uniqueField) {
                if(unfield.equals(field)){
                    skipPrimary = true;
                }
            }
            
            if(skipPrimary){
               continue;
            }
            
            String s = "\n"
                    + "    public java.util.List<" + ClassGeneratorUtil.humanize(getClassName().substring(3)) + "> findBy" + ClassGeneratorUtil.humanize(field) + "(" + type + " " + ClassGeneratorUtil.humanizeToField(field) + ") {\n"
                    + "        return findProcessing(\"" + field + "\", " + ClassGeneratorUtil.humanizeToField(field) + ");\n"
                    + "    }";

            stringClass.append(s).append("\n");
        }

        stringClass.append("\n");
    }

    
}
